package com.ccdc.lbcmsystem.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;


@Configuration
public class ShiroConfig {

    //ShiroFilterFactorBean      3
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultSecurityManager defaultSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultSecurityManager);
        //添加shiro的内置过滤器
        /*
            anon：无需认证就可以访问
            authc：必须认证了才能访问
            user：必须拥有“记住我”功能才能用
            perms：拥有对某个资源的权限才能访问
            role：拥有某个角色权限才能访问
         */
        LinkedHashMap<String, Filter> filtersMap = new LinkedHashMap<>();
        filtersMap.put("roles", new RoleFilter());
        bean.setFilters(filtersMap);
        Map<String, String> filterMap = new LinkedHashMap<>();

        filterMap.put("/giftList","roles[0,1,2]");
        filterMap.put("/searchGift","roles[0,1,2]");
        filterMap.put("/toGiftAdd","roles[0,1,2]");
        filterMap.put("/giftAdd","roles[0,1,2]");
        filterMap.put("/giftLookInfo/*/","roles[0,1,2]");
        filterMap.put("/searchGift","roles[0,1,2]");
        filterMap.put("/toGiftAdd","roles[0,1,2]");
        filterMap.put("/giftAdd","roles[0,1,2]");
        filterMap.put("/toGiftChecked","roles[0,1,2]");
        filterMap.put("/toGiftUnChecked","roles[0,1,2]");


        filterMap.put("/giftExamine","roles[0,2]");
        filterMap.put("/giftExamineAgree","roles[0,2]");
        filterMap.put("/searchUnauditedGift","roles[0,2]");
        filterMap.put("/giftExamineRefuse","roles[0,2]");




        //乡镇管理者、乡镇工作人员、村委会工作人员和村委会管理员可以访问的界面
        filterMap.put("/elderlyList","roles[0,6,7,2]");
        filterMap.put("/searchElderly","roles[0,6,7,2]");
        filterMap.put("/elderlyLookInfo/*/","roles[0,6,7,2]");
        //村委会人员才能访问的页面
        filterMap.put("/elderlyAdd","roles[6,7]");
        filterMap.put("/toElderlyAdd","roles[6,7]");
        filterMap.put("/toElderlyEdit/*/","roles[6,7]");
        filterMap.put("/elderlyEdit","roles[6,7]");
        filterMap.put("/elderlyDelete","roles[6,7]");
        //只有村委会管理人员才能删除独居老人信息审核
        filterMap.put("/delElderlyExamine","roles[7]");


        //乡镇管理者、乡镇工作人员、军人办工作人员和军人办管理员可以访问的界面
        filterMap.put("/veteranList","roles[0,4,2,5]");
        filterMap.put("/searchVeteran","roles[0,4,2,5]");
        filterMap.put("/veteranLookInfo/*/","roles[0,4,2,5]");
        //军人办人员才能访问的页面
        filterMap.put("/toVeteranAdd","roles[4,5]");
        filterMap.put("/veteranAdd","roles[4,5]");
        filterMap.put("/toVeteranEdit/*/","roles[4,5]");
        filterMap.put("/veteranEdit","roles[4,5]");
        filterMap.put("/veteranDelete","roles[4,5]");
        //只有军人办管理员才能进行删除留守儿童信息审核
        filterMap.put("/delVeteranExamine","roles[5]");


        //乡镇管理员的管理权限
        filterMap.put("/adminList","roles[2]");
        filterMap.put("/toAdminAdd","roles[2]");
        filterMap.put("/adminAdd","roles[2]");
        filterMap.put("/toAdminEdit/*/","roles[2]");
        filterMap.put("/adminEdit","roles[2]");
        filterMap.put("/adminDelete","roles[2]");
        filterMap.put("/enableUser","roles[2]");
        filterMap.put("/disableUser","roles[2]");
        filterMap.put("/initializePwd","roles[2]");//初始化密码
        filterMap.put("/searchUser","roles[2]");

        //乡镇管理者、乡镇工作人员、妇联工作人员和妇联管理员可以访问的界面
        filterMap.put("/childrenList","roles[0,1,2,3]");
        filterMap.put("/searchChildren","roles[0,1,2,3]");
        filterMap.put("/childrenLookInfo/*/","roles[0,1,2,3]");
        //只有妇联人员才能删除留守儿童信息
        filterMap.put("/toChildrenAdd","roles[1,3]");
        filterMap.put("/childrenAdd","roles[1,3]");
        filterMap.put("/toChildrenEdit/*/","roles[1,3]");
        filterMap.put("/childrenEdit","roles[1,3]");
        filterMap.put("/childrenDelete","roles[1,3]");
        //只有妇联管理员才能进行删除留守儿童信息审核
        filterMap.put("/delChildrenExamine","roles[3]");

        filterMap.put("/communityList","roles[0,2]");
        filterMap.put("/toCommunityAdd","roles[0,2]");
        filterMap.put("/communityDelete","roles[0,2]");
        filterMap.put("/toCommunityEdit/*/","roles[0,2]");
        filterMap.put("/communityEdit","roles[0,2]");
        filterMap.put("/searchCommunity","roles[0,2]");
        filterMap.put("/regExamine","roles[0,2]");
        filterMap.put("/searchRegExamine","roles[0,2]");

        filterMap.put("/cancelExamine","roles[0,2]");
        filterMap.put("/feedBack","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/notice","roles[0,2]");
        filterMap.put("/noticeAdd","roles[0,2]");
        filterMap.put("/toNoticeAdd","roles[0,2]");
        filterMap.put("/editNotice","roles[0,2]");
        filterMap.put("/upNotice","roles[0,2]");
        filterMap.put("/downNotice","roles[0,2]");
        filterMap.put("/delNotice","roles[0,2]");
        filterMap.put("/toNoticeEdit/*/","roles[0,2]");
        filterMap.put("/noticeLookInfo/*/","roles[0,2]");
        filterMap.put("/feedBackManage","roles[0,2]");
        filterMap.put("/index","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/userInfo/*/*","roles[0,1,2]");
        filterMap.put("/userEdit","roles[0,1,2]");
        filterMap.put("/editPwd","roles[0,1,2]");
        filterMap.put("/toEditPwd/*/","roles[0,1,2]");
        filterMap.put("/cancellation","roles[0,1,2]");
        filterMap.put("/cancelExamine/*/","roles[0,2]");

        filterMap.put("/giftListDerive","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/GiftDerive","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/childrenGiftDerive","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/elderlyGiftDerive","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/veteranGiftDerive","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/giftList","roles[0,1,2,3,4,5,6,7]");

        filterMap.put("/childrenGiftList","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/elderlyGiftList","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/veteranGiftList","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/searchGift","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/searchElderlyGift","roles[0,1,2,3,4,5,6,7]");

        filterMap.put("/searchChildrenGift","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/searchVeteranGift","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toChildrenGiftAdd","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/childrenGiftAdd","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toElderlyGiftAdd","roles[0,1,2,3,4,5,6,7]");

        filterMap.put("/elderlyGiftAdd","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toVeteranGiftAdd","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/veteranGiftAdd","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/childrenGiftLookInfo/*/","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/elderlyGiftLookInfo/*/","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/veteranGiftLookInfo/*/","roles[0,1,2,3,4,5,6,7]");

        filterMap.put("/giftDelete/*/","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/elderlyGiftDelete/*/","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/childrenGiftDelete/*/","roles[0,1,2,3,4,5,6,7]");

        filterMap.put("/veteranGiftDelete/*/","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toGiftChecked","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toChildrenGiftChecked","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toElderlyGiftChecked","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toVeteranGiftChecked","roles[0,1,2,3,4,5,6,7]");

        filterMap.put("/toChildrenGiftUnChecked","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toElderlyGiftUnChecked","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toVeteranGiftUnChecked","roles[0,1,2,3,4,5,6,7]");
        filterMap.put("/toGiftUnChecked","roles[0,1,2,3,4,5,6,7]");

        bean.setFilterChainDefinitionMap(filterMap);
        //设置登录的请求
        bean.setLoginUrl("/toLogin");

        return bean;
    }
    //DafeultWebSecurityManager  2
    @Bean(name = "securityManager")
    public DefaultSecurityManager getDefaultSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建realm对象,需要自定义类   1
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    //整合shiroDialect：用来整合thymeleaf shiro
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
